package com.example.bigupload.controller;

import com.example.bigupload.service.FileDownloadService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.zip.ZipOutputStream;

/**
 * @Author Js
 * @Description
 * @Date 2024-10-28 20:53
 * @Version 1.0
 **/
@RestController
@RequestMapping("/updown")
public class FileUpdowController {

    @Resource
    private FileDownloadService fileDownloadService;

    @GetMapping("/download/batch")
    public void downloadFiles(HttpServletResponse response){

        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=files.zip");

        try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
            fileDownloadService.downloadFiles(zipOut);
        } catch (IOException e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}
